package edu.dali.enterprise_test.kits;

import edu.dali.enterprise_test.model.vo.Dto;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellUtil;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

public class ExcelProc {

	//private static int columnCount = 10;

	public static ByteArrayOutputStream exportProduct(List<Dto> dtos) throws IOException {
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(100);
		Sheet first = (SXSSFSheet) sxssfWorkbook.createSheet("销售明细");
		String content = "";
		Row frow = first.createRow(0);
		for (int j = 0; j < 8; j++) {
			// 首行
			String val = "";
			switch (j) {
				case 0:
					val = "企业编码";
					break;
				case 1:
					val = "商品名称";
					break;
				case 2:
					val = "规格型号";
					break;
				case 3:
					val = "单位";
					break;
				case 4:
					val = "库存数";
					break;
				case 5:
					val = "商品编码";
					break;
				case 6:
					val = "销售时间";
					break;
			}
			frow.createCell(j).setCellValue(val);
		}
		for (int i = 0; i < dtos.size(); i++) {
			Row row = first.createRow(i + 1);
			Dto rDto = dtos.get(i);
			for (int j = 0; j < 7; j++) {
				content = "";
				switch (j) {
					case 0:
						content = rDto.getAsString("qid");
						break;
					case 1:
						content = rDto.getAsString("spmc");
						break;
					case 2:
						content = rDto.getAsString("ggxh");
						break;
					case 3:
						content = rDto.getAsString("jldw");
						break;
					case 4:
						content = rDto.getAsString("stock");
						break;
					case 5:
						content = rDto.getAsString("spbm");
						break;
					case 6:
						content = rDto.getAsDateString("createtime","yyyy-MM-dd");
						break;
				}
				CellUtil.createCell(row, j, content);

			}
		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		sxssfWorkbook.write(bos);
		bos.close();
		bos.close();
		return bos;
	}


	/**
	 * 导出销售数据Excel
	 * @param dtos
	 * @return
	 * @throws IOException
	 */
	public static ByteArrayOutputStream exportSale(List<Dto> dtos) throws IOException {
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(100);
		Sheet first = (SXSSFSheet) sxssfWorkbook.createSheet("销售明细");
		String content = "";
		Row frow = first.createRow(0);
		for (int j = 0; j < 11; j++) {
			// 首行
			String val = "";
			switch (j) {
			case 0:
				val = "单据号";
				break;
			case 1:
				val = "商品名称";
				break;
			case 2:
				val = "规格型号";
				break;
			case 3:
				val = "门店号";
				break;
			case 4:
				val = "门店名称";
				break;
			case 5:
				val = "数量";
				break;
			case 6:
				val = "金额";
				break;
			case 7:
				val = "销售时间";
				break;
			case 8:
				val = "发票号码";
				break;
			case 9:
				val = "企业编号";
				break;
			}
			frow.createCell(j).setCellValue(val);
		}
		for (int i = 0; i < dtos.size(); i++) {
			Row row = first.createRow(i + 1);
			Dto rDto = dtos.get(i);
			for (int j = 0; j < 11; j++) {
				content = "";
				switch (j) {
					case 0:
						content = rDto.getAsString("billno");
						break;
					case 1:
						content = rDto.getAsString("spmc");
						break;
					case 2:
						content = rDto.getAsString("ggxh");
						break;
					case 3:
						content = rDto.getAsString("storeno");
						break;
					case 4:
						content = rDto.getAsString("storename");
						break;
					case 5:
						content = rDto.getAsString("sl");
						break;
					case 6:
						content = rDto.getAsString("amount");
						break;
					case 7:
						content = rDto.getAsDateString("createtime","yyyy-MM-dd");
						break;
					case 8:
						content = rDto.getAsString("FP_HM");
						break;
					case 9:
						content = rDto.getAsString("qid");
						break;
				}
				CellUtil.createCell(row, j, content);

			}
		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		sxssfWorkbook.write(bos);
		bos.close();
		bos.close();
		return bos;
	}

	/**
	 * 导出发票数据
	 * @param dtos
	 * @return
	 * @throws IOException
	 */
	public static ByteArrayOutputStream exportInvoice(List<Dto> dtos) throws IOException {
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(100);
		Sheet first = (SXSSFSheet) sxssfWorkbook.createSheet("销售明细");
		String content = "";
		Row frow = first.createRow(0);
		for (int j = 0; j < 14; j++) {
			// 首行
			String val = "";
			switch (j) {
				case 0:
					val = "购方名称";
					break;
				case 1:
					val = "购方税号";
					break;
				case 2:
					val = "商品名称";
					break;
				case 3:
					val = "商品编码";
					break;
				case 4:
					val = "规格型号";
					break;
				case 5:
					val = "数量";
					break;
				case 6:
					val = "金额";
					break;
				case 7:
					val = "税额";
					break;
				case 8:
					val = "价税合计";
					break;
				case 9:
					val = "单据号";
					break;
				case 10:
					val = "发票代码";
					break;
				case 11:
					val = "发票号码";
					break;
				case 12:
					val = "开票日期";
					break;
				case 13:
					val = "店名";
					break;
			}
			frow.createCell(j).setCellValue(val);
		}
		for (int i = 0; i < dtos.size(); i++) {
			Row row = first.createRow(i + 1);
			Dto rDto = dtos.get(i);
			for (int j = 0; j < 14; j++) {
				content = "";
				switch (j) {
					case 0:
						content = rDto.getAsString("ghfmc");
						break;
					case 1:
						content = rDto.getAsString("ghfnsrsbh");
						break;
					case 2:
						content = rDto.getAsString("spmc");
						break;
					case 3:
						content = rDto.getAsString("spbm");
						break;
					case 4:
						content = rDto.getAsString("ggxh");
						break;
					case 5:
						content = rDto.getAsString("spsl");
						break;
					case 6:
						content = rDto.getAsString("spje");
						break;
					case 7:
						content = rDto.getAsString("se");
						break;
					case 8:
						content = rDto.getAsString("jshj");
						break;
					case 9:
						content = rDto.getAsString("lsh");
						break;
					case 10:
						content = rDto.getAsString("FP_DM");
						break;
					case 11:
						content = rDto.getAsString("FP_HM");
						break;
					case 12:
						content = rDto.getAsString("KPRQ");
						break;
					case 13:
						content = rDto.getAsString("storename");
						break;

				}
				CellUtil.createCell(row, j, content);

			}
		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		sxssfWorkbook.write(bos);
		bos.close();
		bos.close();
		return bos;
	}

	/**
	 * 导出手工发票数据Excel
	 * @param dtos
	 * @return
	 * @throws IOException
	 */
	public static ByteArrayOutputStream exportManualInvoice(List<Dto> dtos) throws IOException {
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook(100);
		Sheet first = (SXSSFSheet) sxssfWorkbook.createSheet("手工明细");
		String content = "";
		Row frow = first.createRow(0);
		for (int j = 0; j < 16; j++) {
			// 首行
			String val = "";
			switch (j) {
				case 0:
					val = "购方名称";
					break;
				case 1:
					val = "购方税号";
					break;
				case 2:
					val = "地址电话";
					break;
				case 3:
					val = "银行账号";
					break;
				case 4:
					val = "商品名称";
					break;
				case 5:
					val = "商品企业编号";
					break;
				case 6:
					val = "商品编码";
					break;
				case 7:
					val = "规格型号";
					break;
				case 8:
					val = "计量单位";
					break;
				case 9:
					val = "数量";
					break;
				case 10:
					val = "单价";
					break;
				case 11:
					val = "金额";
					break;
				case 12:
					val = "税率";
					break;
				case 13:
					val = "税额";
					break;
				case 14:
					val = "创建时间";
					break;
			}
			frow.createCell(j).setCellValue(val);
		}
		for (int i = 0; i < dtos.size(); i++) {
			Row row = first.createRow(i + 1);
			Dto rDto = dtos.get(i);
			for (int j = 0; j < 16; j++) {
				content = "";
				switch (j) {
					case 0:
						content = rDto.getAsString("khmc");
						break;
					case 1:
						content = rDto.getAsString("khsh");
						break;
					case 2:
						content = rDto.getAsString("dzdh");
						break;
					case 3:
						content = rDto.getAsString("yhzh");
						break;
					case 4:
						content = rDto.getAsString("spmc");
						break;
					case 5:
						content = rDto.getAsString("qid");
						break;
					case 6:
						content = rDto.getAsString("spbm");
						break;
					case 7:
						content = rDto.getAsString("ggxh");
						break;
					case 8:
						content = rDto.getAsString("jldw");
						break;
					case 9:
						content = rDto.getAsString("sl");
						break;
					case 10:
						content = rDto.getAsString("dj");
						break;
					case 11:
						content = rDto.getAsString("je");
						break;
					case 12:
						content = rDto.getAsString("slv");
						break;
					case 13:
						content = rDto.getAsString("se");
						break;
					case 14:
						content = rDto.getAsDateString("createtime" , "yyyy-MM-dd");
						break;
				}
				CellUtil.createCell(row, j, content);

			}
		}
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		sxssfWorkbook.write(bos);
		bos.close();
		bos.close();
		return bos;
	}

}
